<?php

namespace app\frontend\controllers;

use app\models\Ins;
use app\models\RecordUpkeep;
use Phalcon\Paginator\Adapter\Model as Paginator;
use app\frontend\forms\BaseInsSearchForm;
use app\frontend\forms\RcdUpkeepSearchForm;

class RecordUpkeepController extends ControllerBase
{
    public function initialize(){
        parent::initialize();
        $this->assets->addJs('pc/js/setwindow.js');
        $this->view->setTemplateBefore('information');
    }

    public function indexAction()
    {
        $this->view->title = '保养记录';
        $numberPage = 1;
        if ($this->request->isPost()) {
            $this->persistent->parameters = $this->request->hasPost('reset') ? []:$this->request->getPost();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $insForm = new BaseInsSearchForm();
        $rcdForm = new RcdUpkeepSearchForm();

        $this->persistent->parameters?$insForm->bind($this->persistent->parameters,$insForm):$insForm->clear();
        $this->persistent->parameters?$rcdForm->bind($this->persistent->parameters,$rcdForm):$rcdForm->clear();

        $insQuery = $insForm->getQuery(Ins::query());
        $inss = $insQuery->execute();
        $ins_ids = [];
        foreach($inss as $ins){
            $ins_ids[] = $ins->pid;
        }
        $rcdQuery = $rcdForm->getQuery();
        $rcdUpkeeps = $rcdQuery->inWhere('ins_id',$ins_ids)->execute();
        $paginator = new Paginator([
            'data' => $rcdUpkeeps,
            'limit'=> 5,
            'page' => $numberPage
        ]);

        $this->view->page = $paginator->getPaginate();
        $this->view->insForm = $insForm;
        $this->view->rcdForm = $rcdForm;
    }
}